<?php
/**
 * Created by JetBrains PhpStorm.
 * User: guansong
 * Date: 17/2/21
 * Time: 上午9:59
 * To change this template use File | Settings | File Templates.
 */

namespace app\index\controller;


use think\Db;

class News {
    public function add()
    {
        return view();
//        return "欢迎使用新闻添加功能！";
    }

    /**
     * 保存新闻
     */
    public function save($title,$content,$newstype)
    {
   //index.php/index/News/save?title=中国&content=人民&newstype=1
        $data = ["title"=>$title,"content"=>$content,"newsTypeid"=>$newstype];
//        Db::table("zy_news")->insert($data);
        $id = db("news")->insertGetId($data);
        if($id>0)
        {
            echo "添加成功！最新的编号为：" . $id;
        }
        else
        {
            echo "添加失败！";
        }
    }
    public function update($title=null,$newsid=null)
    {
        //index.php/index/News/update?title=心的标题&newid=2
        $model = db("news");
        $r = $model->where("id",$newsid)->update(["title"=>$title]);
        //输出执行的 sql 语句
        echo $model->getLastSql() ;
        if($r > 0)
        {
            echo "更新成功！";
        }
        else
        {
            echo "更新失败！";
        }
    }
}